#!/bin/csh -f
# CopyRight:    Copyright(C): dGB Beheer B. V. 2013
# Description:  Script to scan zip files
# Author:       Nageswara
# Date:         April 2013
# RCS:          $Id$

if ( $#argv != 2 ) then
    echo "Usage $0 <directory to scan zip file(s)> <all/single zipfile name>"
    exit
endif

set dirtoscan=$1
set pkgstoscan=$2
if( -d $dirtoscan ) then
    cd $dirtoscan
else
    echo "Entered directory ($dirtoscan) for virus scan does not exist"
    exit 0
endif

set pkgs=""
if( $pkgstoscan == all ) then
    set pkgs=`ls *.zip`
else
    set pkgs=$pkgstoscan
endif
echo "Packages to scan: $pkgs"

rm -rf scaninfo.log

foreach pkg ( ${pkgs} )
    set instdir=`zipinfo -1 ${pkg} | sed -e 's@/.*@@' | uniq`
    echo "DIR: $instdir"
    echo "Scaning $pkg ..."
    avgscan -a $pkg -r scan_report.log
    if ( $status != 0 ) then
	echo "Failed to scan package $pkg. Scanning stopped!"
	echo "Failed to scan $pkg" >> scaninfo.log
	exit 1
    endif

    set filename=${pkg}.virusscan.log
    awk '{ if ($2!="command:") print $0 }' scan_report.log > $filename
    rm -rf "$instdir"
    mkdir "$instdir" "$instdir/relinfo"
    mv "$filename" "$instdir/relinfo/."
    rm -rf scan_report.log

    zip $pkg "$instdir/relinfo/$filename"
    if ( $status != 0 ) then
	echo "Zip failed. Scanning stopped!"
	echo "Zip failed at $pkg" >> scaninfo.log
	exit 1
    endif

    echo "$pkg scanned successfully" >> scaninfo.log
    rm -rf "$instdir"
end

cat scaninfo.log
cd ..

